//
//  AFURLConnectionOperation+MPNetWork.m
//  MiaoPai
//
//  Created by jiaxuzhou on 2017/5/24.
//  Copyright © 2017年 jiaxuzhou. All rights reserved.
//

#import "MPCURLSessionTask+MPCNetWork.h"
#import <objc/runtime.h>

/// 错误信息提示绑定属性key
NSString * const MPNetWorkIsShowToastView4NetWorkState4Operation = @"MPNetWorkIsShowToastView4NetWorkState4Operation";

/// cession过期绑定属性key
NSString * const MPNetWorkSessionTimeoutBlock4Operation = @"MPNetWorkSessionTimeoutBlock4Operation";

/// 请求返回的状态绑定属性key
NSString * const MPNetWorkResaultState4Operation = @"MPNetWorkResaultState4Operation";

/// 错误信息绑定属性key
NSString * const MPNetWorkResaultMessage4Operation = @"MPNetWorkResaultMessage4Operation";

/// 网络请求结果类型绑定属性key
NSString * const MPNetWorkResponseType4Operation = @"MPNetWorkResponseType4Operation";

/// 网络是否被取消绑定属性key
NSString * const MPNetWorkGeIsCancel4Operation = @"MPNetWorkGeIsCancel4Operation";

/// 网络错误信息对象绑定属性key
NSString * const MPNetWorkErrorCode4Operation = @"MPNetWorkErrorCode4Operation";

/// 网络唯一ID绑定属性key
NSString * const MPNetWorkUnique4Operation = @"MPNetWorkUnique4Operation";

/// oldAPi
NSString *const MPNetWorkOldApi4Operation = @"MPNetWorkOldApi4Operation";
/// response加密方式
NSString *const MPNetWorkRespSecType4Operation = @"MPNetWorkRespSecType4Operation";


@implementation NSURLSessionTask (MPNetWork)


- (void)setIsShowToastView4NetWorkState:(BOOL)isShowToastView4NetWorkState
{
    objc_setAssociatedObject(self, (__bridge const void *)(MPNetWorkIsShowToastView4NetWorkState4Operation), @(isShowToastView4NetWorkState), OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (BOOL)isShowToastView4NetWorkState
{
    id isShowToastView4NetWorkState = objc_getAssociatedObject(self, (__bridge const void *)(MPNetWorkIsShowToastView4NetWorkState4Operation));
    if ([isShowToastView4NetWorkState isKindOfClass:[NSNumber class]])
    {
        return [isShowToastView4NetWorkState boolValue];
    }
    
    return YES;
}


- (void)setResponseType:(MPE_NetResponseType)responseType
{
     objc_setAssociatedObject(self, (__bridge const void *)(MPNetWorkResponseType4Operation), @(responseType), OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (MPE_NetResponseType)responseType
{
    id responseType = objc_getAssociatedObject(self, (__bridge const void *)(MPNetWorkResponseType4Operation));
    if ([responseType isKindOfClass:[NSNumber class]])
    {
        return [responseType integerValue];
    }
    
    return MPE_NetResponseType_Json;
}

- (void)setResaultState:(NSString *)state
{
    objc_setAssociatedObject(self, (__bridge const void *)(MPNetWorkResaultState4Operation), state, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (NSString *)resaultState
{
    id resaultState = objc_getAssociatedObject(self, (__bridge const void *)(MPNetWorkResaultState4Operation));
    if (resaultState && [resaultState isKindOfClass:[NSString class]])
    {
        return resaultState;
    }
    
    return nil;
}

- (void)setResaultMessage:(NSString *)message
{
    objc_setAssociatedObject(self, (__bridge const void *)(MPNetWorkResaultMessage4Operation), message, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (NSString *)resaultMessage
{
    id resaultMessage = objc_getAssociatedObject(self, (__bridge const void *)(MPNetWorkResaultMessage4Operation));
    if (resaultMessage && [resaultMessage isKindOfClass:[NSString class]])
    {
        return resaultMessage;
    }
    
    return nil;
}

/// 请求结果message字符串
- (void)setResaultError:(NSError *)error
{
    if (error)
    {
        objc_setAssociatedObject(self, (__bridge const void *)(MPNetWorkErrorCode4Operation), error, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
}

- (NSError *)resaultError
{
    id code = objc_getAssociatedObject(self, (__bridge const void *)(MPNetWorkErrorCode4Operation));
    if (code && [code isKindOfClass:[NSError class]])
    {
        return code;
    }
    
    return nil;
}

- (void)setSessionTimeoutBlock:(void (^)(MPE_NetWorkSessionState state))sessionTimeoutBlock
{
    if (sessionTimeoutBlock)
    {
        objc_setAssociatedObject(self, (__bridge const void *)(MPNetWorkSessionTimeoutBlock4Operation), sessionTimeoutBlock, OBJC_ASSOCIATION_COPY_NONATOMIC);
    }
}

- (void (^)(MPE_NetWorkSessionState state))sessionTimeoutBlock
{
    id sessionTimeoutBlock = objc_getAssociatedObject(self, (__bridge const void *)(MPNetWorkSessionTimeoutBlock4Operation));
    if (sessionTimeoutBlock)
    {
        return sessionTimeoutBlock;
    }
    
    return nil;
}

- (void)setIsCancel:(BOOL)isCancel
{
    objc_setAssociatedObject(self, (__bridge const void *)(MPNetWorkGeIsCancel4Operation), @(isCancel), OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (BOOL)getIsCancel
{
    id isCancel = objc_getAssociatedObject(self, (__bridge const void *)(MPNetWorkGeIsCancel4Operation));
    if ([isCancel isKindOfClass:[NSNumber class]])
    {
        return [isCancel boolValue];
    }
    
    return NO;
}

/// 设置唯一的ID
- (void)setReqUnique:(id)unique
{
    if (unique)
    {
        objc_setAssociatedObject(self, (__bridge const void *)(MPNetWorkUnique4Operation), unique, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
}

- (id)reqUnique
{
    id unique = objc_getAssociatedObject(self, (__bridge const void *)(MPNetWorkUnique4Operation));
    
    return unique;

}

/// 设置是否是老的api
- (void)setOldApi:(BOOL)oldApi
{
     objc_setAssociatedObject(self, (__bridge const void *)(MPNetWorkOldApi4Operation), @(oldApi), OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (BOOL)oldApi
{
    id oldApi = objc_getAssociatedObject(self, (__bridge const void *)(MPNetWorkOldApi4Operation));
    if ([oldApi isKindOfClass:[NSNumber class]])
    {
        return [oldApi boolValue];
    }
    
    return YES;
}

/// 设置response加密方式
- (void)setRespEdsType:(MPE_NetRespSecType)respEdsType
{
    objc_setAssociatedObject(self, (__bridge const void *)(MPNetWorkRespSecType4Operation), @(respEdsType), OBJC_ASSOCIATION_COPY_NONATOMIC);

}

- (MPE_NetRespSecType)respEdsType
{
    id respEdsType = objc_getAssociatedObject(self, (__bridge const void *)(MPNetWorkRespSecType4Operation));
    if ([respEdsType isKindOfClass:[NSNumber class]])
    {
        return [respEdsType integerValue];
    }
    
    return MPE_NetRespSecType_Json;
}


@end
